﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Repository;
using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Service
{
    [UseDI(ServiceLifetime.Scoped)]
    public class ExtVideoService
    {
        private readonly ExtVideoRepository _extVideo;
        private readonly ICacheManager cacheManager;
        private readonly string cacheKey;
        private readonly string cacheListKey;
        public ExtVideoService(ExtVideoRepository  extVideoRepository, ICacheManager CacheManager)
        {
            cacheManager = CacheManager;
            _extVideo = extVideoRepository;
            cacheKey = cacheManager.CacheName() + "video_Info_";
            cacheListKey = cacheManager.CacheName() + "video_List_";
        }

        /// <summary>
        /// 前台查询一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ViewExtVideoCateAndContent> GetAsync(int? id)
        {
            ViewExtVideoCateAndContent video = cacheManager.Get<ViewExtVideoCateAndContent>(cacheKey + id);
            if (video == null)
            {
                video = await _extVideo.GetOneAsync(id);
                cacheManager.Set(cacheKey + id, video, TimeSpan.FromHours(10));//缓存2秒
            }
            await _extVideo.IncHits(id);//浏览+1
            return video;
        }

        /// <summary>
        /// 后台编辑一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ExtVideo> GetEditAsync(int? id)
        {
            return await _extVideo.GetAsync(id);
        }

        /// <summary>
        /// 按CID获取全部列表 后台
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<Page<ViewExtVideoCateAndContent>> GetListByCidAdminAsync(int? cid, int page, int limit, int uid, string keyWord, int status)
        {
            return await _extVideo.GetListAsync(cid, page, limit, "video.Id desc", keyWord, status, uid);
        }

        /// <summary>
        /// 获取全部列表  前台  获取 status=1 列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<Page<ViewExtVideoCateAndContent>> GetListAsync(int page, int limit)
        {
            return await _extVideo.GetListAsync(0, page, limit, "video.Id desc", "", 1);
        }

        /// <summary>
        /// 按CID获取全部列表 获取 status=1 列表  前台
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<Page<ViewExtVideoCateAndContent>> GetListByCidAsync(int cid, int page, int limit)
        {
            return await _extVideo.GetListAsync(cid, page, limit, "video.Id desc", "", 1);
        }

        /// <summary>
        /// 搜索列表 前台
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public async Task<Page<ViewExtVideoCateAndContent>> SearchPageList(int page, int limit, string keyword)
        {
            return await _extVideo.GetListAsync(page, limit, keyword);

        }

        /// <summary>
        /// 按CID获取指定条数列表 前台
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="limit"></param>
        /// <param name="cacheName"></param>
        /// <param name="cacheTime"></param>
        /// <param name="att"></param>
        /// <param name="orderby"></param>
        /// <returns></returns>
        public async Task<List<ViewExtVideoCateAndContent>> VideoList(string cid, int limit, string cacheName, int cacheTime = 600, int att = 0, string orderby = "Id desc")
        {
            List<ViewExtVideoCateAndContent> article = cacheManager.Get<List<ViewExtVideoCateAndContent>>(cacheListKey + cacheName);
            if (article == null)
            {
                article = await _extVideo.GetListByLimtAsync(cid, limit, att, "video." + orderby);
                if (cacheName != null)
                    cacheManager.Set(cacheListKey + cacheName, article, TimeSpan.FromSeconds(cacheTime));//缓存
            }
            return article;
        }


        public virtual async Task<object> InsertvideoAsync(ExtVideo  video)
        {
            return await _extVideo.InsertAsync(video);
        }

        public virtual async Task<int> UpdateVideoAsync(ExtVideo extvideo)
        {
            cacheManager.RemoveCacheAsync(cacheKey + extvideo.Id);
            ExtVideo video = await _extVideo.GetAsync(extvideo.Id);
            video.Img = extvideo.Img;
            video.Cid = extvideo.Cid;   
            video.Title = extvideo.Title;
            video.Examine = extvideo.Examine;
            video.Content= extvideo.Content;
            video.Status = extvideo.Status;
            video.AttA = extvideo.AttA;
            video.AttB = extvideo.AttB;
            video.AttC = extvideo.AttC;
            return await _extVideo.UpdateAsync(video);
        }

        public virtual async Task<int> UpdateAttAsync(int id, int status, string att)
        {
            return await _extVideo.UpdateAttAsync(id, status, att);
        }


        /// <summary>
        /// 删除一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<int> DelAsync(int id)
        {
            if (await _extVideo.DeleteByPriKeyAsync(id) > 0)
            {
                cacheManager.RemoveCacheAsync(cacheKey + id);
                return 1;
            }
            return 0;
        }
    }
}
